package com.lfg.leetcode.leetcode200;

import com.lfg.leetcode.leetcode0.TreeNode;

public class No230kthSmallest {

    static int counter = 0;

    public static void main(String[] args) {
        System.out.println(kthSmallest(TreeNode.stringToTreeNode("[3,1,4,null,2]"), 2));
    }

    public static int kthSmallest(TreeNode root, int k) {
        counter = k;
        return dfs(root);
    }

    public static Integer dfs(TreeNode root) {

        if (root == null) {
            return null;
        }
        Integer ret = dfs(root.left);
        if (null != ret) {
            return ret;
        }
        counter--;
        if (counter == 0) {
            return root.val;
        }
        ret = dfs(root.right);
        if (null != ret) {
            return ret;
        }

        return null;
    }
}
